import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { EmployeeComponent } from 'src/app/components/employee/employee.component';

import { LeavesComponent } from 'src/app/components/leaves/leaves.component';
import { ReleaseTaskComponent } from 'src/app/components/release-task/release-task.component';
import { AttendanceComponent } from 'src/app/components/attendance/attendance.component';
import { MailListComponent } from 'src/app/components/mail-list/mail-list.component';
import { GoodsComponent } from 'src/app/components/goods/goods.component';
import { OtherleavesComponent } from 'src/app/components/otherleaves/otherleaves.component';
import { ReminderComponent } from 'src/app/components/reminder/reminder.component';
import { ReceiveTaskComponent } from 'src/app/components/receive-task/receive-task.component';
import { PasswordComponent } from 'src/app/components/password/password.component';
import { PersonalComponent } from 'src/app/components/personal/personal.component';

import { PersonManagementComponent } from 'src/app/components/person-management/person-management.component';
import { Router } from '@angular/router';
import { EditComponent } from 'src/app/components/edit/edit.component';
import { AdoptComponent } from 'src/app/components/adopt/adopt.component';
import { SalaryComponent } from 'src/app/components/salary/salary.component';
import { PlansComponent } from 'src/app/components/plans/plans.component';
import { ApprovalComponent } from 'src/app/components/approval/approval.component';
import { InterfaceService } from 'src/app/services/interface.service';
import { IfStmt } from '@angular/compiler';
import { THIS_EXPR } from '@angular/compiler/src/output/output_ast';
import { CartsComponent } from 'src/app/components/carts/carts.component';

@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.css']
})
export class IndexComponent implements OnInit {
  lists: any = [];
  // { name: 'leaves' },                     //icon-qingjiadan            
  // { name: 'releaseTask' },                //icon-zixun
  // { name: 'reminder' },                   //icon-duihua
  // { name: 'attendance' },                 //icon-qiandao1
  // { name: 'receiveTask' },                //icon-renwuguanli

  // { name: 'mailList' },                   //icon-contact-icon
  // { name: 'edit' },                       //icon-bianji
  // { name: 'goods' },                      //icon-bangongyongpin
  // { name: 'otherleaves' },                //icon-qingjiadan 
  // { name: 'plans' },                      //icon-richeng
  // { name: 'personManagement' },           //icon-renshiguanli
  // { name: 'adopt' },                      //icon-bangongyongpin
  // { name: 'approval' },                   //icon-drxx27
  // { name: 'salary' },                     //icon-renminbi
  state: boolean = false;
  img: any;
  constructor(private elementRef: ElementRef, public router: Router, private inter: InterfaceService) { }
  ngOnInit() {
    // this.img=JSON.parse(sessionStorage.getItem('USER')).image;
    // if(!JSON.parse(sessionStorage.getItem('USER'))){
    //   this.router.navigateByUrl('login');
    // }



  }

  //人员
  @ViewChild(EmployeeComponent, { static: false })
  employee: EmployeeComponent

  //请假申请表
  @ViewChild(LeavesComponent, { static: false })
  leaves: LeavesComponent

  //发布任务
  @ViewChild(ReleaseTaskComponent, { static: false })
  releaseTask: ReleaseTaskComponent

  //考勤表
  @ViewChild(AttendanceComponent, { static: false })
  attendance: AttendanceComponent

  //通讯录
  @ViewChild(MailListComponent, { static: false })
  mailList: MailListComponent

  //办公用品申请表
  @ViewChild(GoodsComponent, { static: false })
  goods: GoodsComponent

  //管理人员见到请假表
  @ViewChild(OtherleavesComponent, { static: false })
  otherleaves: OtherleavesComponent

  //消息提醒
  @ViewChild(ReminderComponent, { static: false })
  reminder: ReminderComponent

  //提交任务
  @ViewChild(ReceiveTaskComponent, { static: false })
  receiveTask: ReceiveTaskComponent

  //修改密码
  @ViewChild(PasswordComponent, { static: false })
  password: PasswordComponent

  //个人资料
  @ViewChild(PersonalComponent, { static: false })
  personal: PersonalComponent

  //计划表
  @ViewChild(PlansComponent, { static: false })
  plans: PlansComponent

  //人事管理
  @ViewChild(PersonManagementComponent, { static: false })
  personManagement: PersonManagementComponent

  //编辑表
  @ViewChild(EditComponent, { static: false })
  edit: EditComponent

  //办公用品领取通过表
  @ViewChild(AdoptComponent, { static: false })
  adopt: AdoptComponent

  //薪资
  @ViewChild(SalaryComponent, { static: false })
  salary: SalaryComponent

  //审批
  @ViewChild(ApprovalComponent, { static: false })
  approval: ApprovalComponent

  //购物车
  @ViewChild(CartsComponent , {static:false})
  carts:CartsComponent


  ngAfterViewInit() {
    this.inter.getAllIcon().subscribe((res: any) => {
      if (res.code == 100) {
        this.lists = res.data;
        setTimeout(() => {
          //获取到页面高度
          let height: number = document.body.clientHeight - 46;
          //获取图标元素

          const li: any = this.elementRef.nativeElement.querySelectorAll(".icon-container li");
          //算出一列最多能放几个
          let count: number = Math.floor(height / 110);
          //累加,当前这列的第几个
          let num: number = 0;
          //第几列
          let w: number = 0;
          //第几行
          let h: number = 0;
          //便利图标元素,修改他的toph和left
          for (let i = 0; i < li.length; i++) {
            //当累加列的个数等于列的最大个数,转到下一列去
            if (num == count) {
              num = 0;
              h = 0;
              w++;
            }

            li[i].style.top = 110 * h + 20 + 'px';
            li[i].style.left = 100 * w + 'px';
            h++;
            num++;
          }
        }, 0);

      }
    })

  }
  open(e: string): void {
    let name = this.lists[e].code;
    console.log(name)
    let obj = this.getModelFactory(name);

    if (obj) {
      obj.show(e);
    }
  }
  start(): void {
    this.state = true;
    setTimeout(() => {
      this.state = false;
    }, 5000);
    console.log(JSON.parse(sessionStorage.getItem('USER')));
  }

  person(): void {
    this.personal.show();
  }

  resetPassword(): void {
    this.password.show();
  }

  exit(): void {
    sessionStorage.clear();
    this.router.navigateByUrl('login');
  }
  getModelFactory(name: string): any {
    console.log(this);
    for (let item in this) {
      if (name == item) {
        return this[item];
      }
    }
    return false;
  }
}
